package com.micro.school.order.service.impl;

import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.order.entity.PayPrepaymentProject;
import com.micro.school.order.mapper.PayPrepaymentProjectMapper;
import com.micro.school.order.service.PayPrepaymentProjectService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 预缴费项目管理(PayPrepaymentProject)表服务实现类
 *
 * @author makejava
 * @since 2025-08-14 17:27:00
 */
@Service("payPrepaymentProjectService")
public class PayPrepaymentProjectServiceImpl implements PayPrepaymentProjectService {

    @Resource
    private PayPrepaymentProjectMapper payPrepaymentProjectMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PayPrepaymentProject queryById(Integer id) {
        return this.payPrepaymentProjectMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param payPrepaymentProject 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public List<PayPrepaymentProject> queryList(PayPrepaymentProject payPrepaymentProject) {
        return this.payPrepaymentProjectMapper.queryAllByLimit(payPrepaymentProject);
    }

    /**
     * 新增数据
     *
     * @param payPrepaymentProject 实例对象
     * @return 实例对象
     */
    @Override
    public PayPrepaymentProject insert(PayPrepaymentProject payPrepaymentProject) {
        // 校验项目名称是否已存在
        PayPrepaymentProject queryParam = new PayPrepaymentProject();
        queryParam.setProjectName(payPrepaymentProject.getProjectName());
        queryParam.setTenantId(payPrepaymentProject.getTenantId());
        queryParam.setDeleteFlag("0");
        List<PayPrepaymentProject> existingProjects = this.payPrepaymentProjectMapper.queryAllByLimit(queryParam);

        if (!existingProjects.isEmpty()) {
            throw new RuntimeException("项目名称已存在，请重新输入！");
        }

        this.payPrepaymentProjectMapper.insert(payPrepaymentProject);
        return payPrepaymentProject;
    }

    /**
     * 修改数据
     *
     * @param payPrepaymentProject 实例对象
     * @return 实例对象
     */
    @Override
    public void update(PayPrepaymentProject payPrepaymentProject) {
        // 校验项目名称是否已存在（排除当前修改的记录）
        PayPrepaymentProject queryParam = new PayPrepaymentProject();
        queryParam.setProjectName(payPrepaymentProject.getProjectName());
        queryParam.setTenantId(payPrepaymentProject.getTenantId());
        queryParam.setDeleteFlag("0");
        List<PayPrepaymentProject> existingProjects = this.payPrepaymentProjectMapper.queryAllByLimit(queryParam);

        // 如果查询到的记录不是当前正在修改的记录，则说明名称重复
        for (PayPrepaymentProject project : existingProjects) {
            if (!project.getId().equals(payPrepaymentProject.getId())) {
                throw new RuntimeException("项目名称已存在，请重新输入！");
            }
        }

        this.payPrepaymentProjectMapper.update(payPrepaymentProject);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.payPrepaymentProjectMapper.deleteById(id) > 0;
    }
}
